package com.yzqp.blog.interceptor;

import org.springframework.stereotype.Component;

@Component
public class IndexInterceptor extends BaseInterceptor {

//    @Autowired
//    MemberService memberService;
//
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //OPTIONS请求直接放行
//        if (RequestMethod.OPTIONS.name().equals(request.getMethod())) {
//            return true;
//        }
//        LogUtil.getBean().info("IndexInterceptor: >>>>>> " + request.getRequestURI());
//
//
//        //获取前端传来的header，名字为Authorization
//        String token = request.getHeader("Authorization");
//        Enumeration<String> list = request.getHeaderNames();
//        LogUtil.getBean().info("token:" + token);
//
//        // 校验token是否为空
//        if (token == null || token.isEmpty()) {
//            responseWrite(response, WebJsonResultUtil.errorLogin("登录超时"));
//            return false;
//        }
//        // 从redis获取用户信息
//        String userinfo = (String) RedisUtil.get(RedisConst.USER_TOKEN + token);
//        if (userinfo == null || userinfo.isEmpty()) {
//            responseWrite(response, WebJsonResultUtil.errorLogin("登录超时"));
//            return false;
//        }
//
//        Member member = null;
//        try {
//            member = (Member) FastJsonUtil.stringToObject(userinfo, Member.class);
//            member = memberService.get(member.getId());
//            if (member == null) {
//                responseWrite(response, WebJsonResultUtil.errorLogin("找不到目标用户"));
//                return false;
//            }
//        } catch (Exception e) {
//            LogUtil.getBean().error(e.getMessage());
//            userinfo = null;
//        }
//        // 缓存信息，供后续使用
//        request.setAttribute("userinfo", member);
//        request.setAttribute("token", token);
//        return true;
//    }
}